package com.sczmgk.hotline_pzh.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.callback.OnRefreshListener;

import java.text.SimpleDateFormat;
import java.util.Date;

public class RefreshListView extends ListView implements OnScrollListener {

	private ProgressBar mLoadMorePb;
	private TextView mLoadMoreTv;
	private int mFirstVisibleItem = -1; // 滚动时屏幕上第一个显示item的position
	private int downY; // 按下时y轴的偏移量
	private int headerViewHeight; // 头布局的高度
	private View headerView; // 头布局对象

	private final int PULL_DOWN_REFRESH = 0; // 下拉刷新状态
	private final int RELEASE_REFRESH = 1; // 释放刷新
	private final int REFRESHING = 2; // 正在刷新中

	private int currentHeaderState = PULL_DOWN_REFRESH; // 当前头布局的状态, 默认为: 下拉刷新
	private ImageView ivLoading; // 动画

	private OnRefreshListener mOnRefreshListener; // 用户的刷新回调事件
	private View footerView; // 脚布局对象
	private int footerViewHeight; // 脚布局高度
	private boolean isLoadingMore = false; // 是否正在加载更多中
	private AnimationDrawable ad;

	private boolean isPullDown = true ;

	public RefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initHeader();
		initFooter();
		setOnScrollListener(this);
		
	}

	/**
	 * 初始化脚布局
	 */
	private void initFooter() {
		footerView = View.inflate(getContext(), R.layout.listview_footer, null);
		mLoadMorePb = (ProgressBar) footerView.findViewById(R.id.pb_loadmore);
		mLoadMoreTv = (TextView) footerView.findViewById(R.id.tv_loadmore);
		footerView.measure(0, 0);
		footerViewHeight = footerView.getMeasuredHeight();
		footerView.setPadding(0, -footerViewHeight, 0, 0);
		addFooterView(footerView,null,false);
	}

	private void initHeader() {
		headerView = View.inflate(getContext(), R.layout.refresh_listview_header, null);
		ivLoading = (ImageView) headerView.findViewById(R.id.iv_loading_more);

		headerView.measure(0, 0);
		headerViewHeight = headerView.getMeasuredHeight();
		headerView.setPadding(0, -headerViewHeight, 0, 0);
		addHeaderView(headerView,null,false);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN: // 按下
			downY = (int) ev.getY();
			break;
		case MotionEvent.ACTION_MOVE: // 移动
			if (isPullDown) {
				onInterceptTouchEvent(ev);// 移动时中止父类的事件
				if (currentHeaderState == REFRESHING) {
					break;
				}
				int moveY = (int) ev.getY();
				int diff = moveY - downY;
				if (mFirstVisibleItem == 0 && diff > 0) { // 当前屏幕是在顶部, 并且是向下拖动
					if (diff > headerViewHeight) {
						diff = headerViewHeight;
					}
					int paddingTop = -headerViewHeight + diff;
					if (paddingTop >= 0 && currentHeaderState == PULL_DOWN_REFRESH) {
						currentHeaderState = RELEASE_REFRESH;
						refreshHeaderState();
					} else if (paddingTop < 0
							&& currentHeaderState == RELEASE_REFRESH) {
						currentHeaderState = PULL_DOWN_REFRESH;
						refreshHeaderState();
					}

					headerView.setPadding(0, paddingTop, 0, 0);
					return true; // 自己响应用户的事件
				}
			}
			break;
		case MotionEvent.ACTION_UP: // 抬起
			if (currentHeaderState == PULL_DOWN_REFRESH) {
				headerView.setPadding(0, -headerViewHeight, 0, 0);
			} else if (currentHeaderState == RELEASE_REFRESH) {

				headerView.setPadding(0, 0, 0, 0);
				currentHeaderState = REFRESHING;
				refreshHeaderState();
				if (mOnRefreshListener != null) {
					mOnRefreshListener.onPullDownRefresh();
				}
			}
			break;
		default:
			break;
		}
		return super.onTouchEvent(ev); // 调用父类默认的滚动效果
	}

	/**
	 * 根据currentHeaderState来刷新头布局的状态
	 */
	private void refreshHeaderState() {
		switch (currentHeaderState) {
		case PULL_DOWN_REFRESH: // 下拉刷新

			break;
		case RELEASE_REFRESH: // 释放刷新


			break;
		case REFRESHING: // 正在刷新中

			ad = (AnimationDrawable) ivLoading.getBackground();
			ad.start();

			break;
		default:
			break;
		}
	}

	/**
	 * 当滚动时调用. firstVisibleItem 滚动时屏幕上第一个可见的item的position visibleItemCount
	 * 屏幕上显示了多少条item totalItemCount ListView的总长度
	 */
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		mFirstVisibleItem = firstVisibleItem;
	}

	/**
	 * 当滚动状态改变时, 回调此方法 scrollState 当前滚动的状态
	 */
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if(!enableLoadMore){
			return;
		}
		if ((scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_FLING)
				&& getLastVisiblePosition() == (getCount() - 1)) {

			if (!isLoadingMore) {
				isLoadingMore = true;
				footerView.setPadding(0, 0, 0, 0); // 显示加载更多  
				setSelection(getCount() - 1);
				if (mOnRefreshListener != null) {
					mOnRefreshListener.onLoadingMore();
				}
			}
		}
	}

	public void setOnRefreshListener(OnRefreshListener listener) {
		mOnRefreshListener = listener;
	}

	/**
	 * 隐藏头布局
	 */
	public void hideHeaderView() {

		headerView.setPadding(0, -headerViewHeight, 0, 0);
		currentHeaderState = PULL_DOWN_REFRESH;
		if (ad != null) {
			ad.stop();
		}
	}

	/**
	 * 获得最新的时间 1990-09-09 09:09:09
	 * 
	 * @return
	 */
	private String getLastUpdateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}

	/**
	 * 隐藏脚布局
	 */
	public void hideFooterView() {
		if(!showNotMore){
			footerView.setPadding(0, -footerViewHeight, 0, 0);
			isLoadingMore = false;
		}
	}

	private boolean enableLoadMore = true;

	public boolean isRefresh(){
		return currentHeaderState == REFRESHING ;
	}

	/**
	 * 设置能否加载更多
	 * @param enableLoadMore
     */
	public void setEnableLoadMore(boolean enableLoadMore){
        this.enableLoadMore = enableLoadMore;
	}

	/**
	 * 设置能否上拉刷新
	 * @param enablePull
	 */
	public void setEnablePullDown(boolean enablePull){
		this.isPullDown = enablePull ;
	}

	public void showNotMore(){
		showNotMore = true;
		enableLoadMore = false;
		footerView.setPadding(0, 0, 0, 0); // 显示加载更多
		mLoadMorePb.setVisibility(View.GONE);
		mLoadMoreTv.setText("");
	}
	public void hideNotMore(){
		footerView.setPadding(0, -footerViewHeight, 0, 0);
		showNotMore = false;
		enableLoadMore = true;
		mLoadMorePb.setVisibility(View.VISIBLE);
		mLoadMoreTv.setText("加载更多...");
	}
	private boolean showNotMore;

	@Override
	public void setOnItemClickListener(OnItemClickListener listener) {

		super.setOnItemClickListener(listener);
	}

	public void setShowNotMore(boolean showNotMore) {
		this.showNotMore = showNotMore;
	}

	public boolean isShowNotMore() {
		return showNotMore;
	}
}
